/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mimicinfolist.h"

//---------------------------------------------------------------------------

MimicInfoList::MimicInfoList(void)
{
}

MimicInfoList::~MimicInfoList(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MimicInfoList::CleanUp(void)
{
	MimicInfo *elem;

	for(itor=theVector.begin(); itor!=theVector.end(); itor++){
		elem = *itor;
		if(elem!=NULL){
			*itor = NULL;
			delete elem;
		}
	}
	theVector.clear();
}

//---------------------------------------------------------------------------

MimicInfo* MimicInfoList::CreateMimicInfo(void)
{
	MimicInfo *elem;

	try{
		elem = new MimicInfo;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

bool MimicInfoList::AddElement(MimicInfo* elemIn)
{
	MimicInfo *elem;
	int cnt;

	if(elemIn==NULL) return true;

	elem = CreateMimicInfo();
	if(elem==NULL) return false;

	elem->LoadFromPointer(elemIn);

	cnt = (int)theVector.size();
	theVector.push_back(elem);
	cnt = (int)theVector.size() - cnt;
	if(cnt!=1){
		delete elem;
		return false;
	}

	return true;
}

//---------------------------------------------------------------------------

int MimicInfoList::GetSize(void)
{
	return (int)theVector.size();
}

MimicInfo* MimicInfoList::GetByIdx(int idxIn)
{
	if(idxIn<0)          return NULL;
	if(idxIn>=GetSize()) return NULL;

	return theVector[idxIn];
}

//---------------------------------------------------------------------------

bool MimicInfoList::InitializeWithDefaultValues(void)
{
	MimicInfo mi;

	if(!mi.Set(__mosWindowsXPSP2)) return false;
	if(!AddElement(&mi)) return false;

	if(!mi.Set(__mosWindows2K3)) return false;
	if(!AddElement(&mi)) return false;

	if(!mi.Set(__mosLinux_2_4)) return false;
	if(!AddElement(&mi)) return false;

	if(!mi.Set(__mosLinux_2_6)) return false;
	if(!AddElement(&mi)) return false;

	if(!mi.Set(__mosSolaris_8)) return false;
	if(!AddElement(&mi)) return false;

	return true;
}

//---------------------------------------------------------------------------
